package com.nl.controller.sms5g.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nl.controller.common.annotation.SystemServiceLog;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.global.GlobalConstants;
import com.nl.controller.common.util.BaseResult;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.sms5g.bean.bo.*;
import com.nl.controller.sms5g.bean.po.SignatureInfo;
import com.nl.controller.sms5g.mapper.Sms5gSignatureMapper;
import com.nl.controller.sms5g.service.MailService;
import com.nl.controller.sms5g.service.Sms5gSignatureService;
import com.nl.controller.sms5g.util.DownloadExcel;
import com.nl.controller.sms5g.util.ExcelUtil;
import com.nl.controller.web.bean.BlackUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.mail.*;
import javax.mail.internet.MimeMessage;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @desc 5G多媒体签名
 * @return null
 * @author l
 * @date 2021/5/27 10:14
 */
@Service
@Slf4j
public class Sms5gSignatureServiceImpl implements Sms5gSignatureService {

    @Autowired
    private Sms5gSignatureMapper sms5gSignatureMapper;


    
    /**
     * @desc 判断签名是否重名
     * @param signName 
     * @return java.lang.Boolean
     * @author l
     * @date 2021/6/17 10:36
     */
    @Override
    public BaseResult<Integer> judgeRepeat(String signName) {
        BaseResult<Integer> baseResult = BaseResult.newFailInstance();
        int count = 0;
        try {
            count = sms5gSignatureMapper.countByName(signName);
            baseResult.setRetCode(GlobalConstants.SUCCESS_STRING);
            baseResult.setRetMsg("签名名称判重成功");
            baseResult.setData(count);
        }catch (Exception e){
            e.printStackTrace();
            baseResult.setRetCode(GlobalConstants.EXCEPTION_STRING);
            baseResult.setRetMsg("签名名称判重出现异常");
        }
        return baseResult;
    }

    /**
     * @desc 新建5G多媒体签名
     * @param createSignatureBo
     * @return com.nl.controller.common.util.BaseResult
     * @author l
     * @date 2021/5/30 21:36
     */
    @Override
    public BaseResult createSignature(CreateSignatureBo createSignatureBo) {
        BaseResult baseResult = BaseResult.newFailInstance();

        String industryType = "";
        String customerType = "";

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
        String extNumber = "";
        String signId = "";
        try{
            signId = sms5gSignatureMapper.qrySignSeq();
            createSignatureBo.setSignId(signId);
            extNumber = sms5gSignatureMapper.find5gExtId();

            createSignatureBo.setSmsChannelNumber("106571108"+createSignatureBo.getCityId()+extNumber);

            sms5gSignatureMapper.createSignature(createSignatureBo);

            SignExcelData excelData = new SignExcelData();
            excelData.setSignId(signId);
            excelData.setEcName("福建新大陆软件工程有限公司");
            excelData.setSendEcName(createSignatureBo.getEcName());
            excelData.setSignName(createSignatureBo.getSignName());
            excelData.setProvinceName("江苏省");
            excelData.setCityName(sms5gSignatureMapper.qryCityName(createSignatureBo.getCityId()).get(0));




            excelData.setExtendNumber(createSignatureBo.getCityId()+extNumber);
            excelData.setServiceNumber(GlobalConstants.SMS_5G_SERVICE_NUMBER);
            industryType = sms5gSignatureMapper.qryIndustryNameById(createSignatureBo.getIndustryType());
            customerType = sms5gSignatureMapper.qryCustomerNameById(createSignatureBo.getCustomerType());

            excelData.setIndustryType(createSignatureBo.getIndustryType()+"—"+industryType);
            excelData.setCustomerType(createSignatureBo.getCustomerType()+"—"+customerType);
            excelData.setAuthorizationId(createSignatureBo.getAuthorizationId());
            //excelData.setOperatorDate(sdf.format(new Date()));

            //新增邮件送审记录
            sms5gSignatureMapper.createRecord(excelData);

            //送审状态变更为已送审
            sms5gSignatureMapper.updateSignSendState(signId,"1");


            baseResult.setRetCode(GlobalConstants.SUCCESS_STRING);
            baseResult.setRetMsg("新建5G多媒体签名成功");


        }catch (Exception e){
            baseResult.setRetCode(GlobalConstants.EXCEPTION_STRING);
            baseResult.setRetMsg("新建5G多媒体签名发生异常"+e.getMessage());
            e.printStackTrace();

        }
        return baseResult;
    }

    /**
     * @desc 变更5G多媒体签名
     * @param updateSignatureBo
     * @return com.nl.controller.common.util.BaseResult
     * @author l
     * @date 2021/5/30 21:56
     */
    @Override
    public BaseResult updateSignature(UpdateSignatureBo updateSignatureBo) {
        BaseResult baseResult = BaseResult.newFailInstance();
        try{
            sms5gSignatureMapper.updateSignature(updateSignatureBo);
            baseResult.setRetCode(GlobalConstants.SUCCESS_STRING);
            baseResult.setRetMsg("变更5G多媒体签名成功");
        }catch (Exception e){
            baseResult.setRetCode(GlobalConstants.EXCEPTION_STRING);
            baseResult.setRetMsg("变更5G多媒体签名发生异常"+e.getMessage());
            e.printStackTrace();

        }
        return baseResult;
    }

    /**
     * @desc 查询签名信息
     * @param
     * @return com.github.pagehelper.PageInfo<com.nl.controller.sms5g.bean.po.SignatureInfo>
     * @author l
     * @date 2021/6/15 10:55
     */
    @Override
    public PageInfo<SignatureInfo>  qrySignatureList(int page,int limit,String cityId,String countyId,String signName,String ecName,String checkState,String industryType,String customerType) {
        PageHelper.startPage(page, limit);
        List<SignatureInfo> signatureInfoList = new ArrayList<>();
        PageInfo<SignatureInfo> pageInfo = null;
        String cityName = "";
        String countyName = "";
        if("25".equals(cityId)){
            cityId = "";
            countyId = "";
        }
        try {
            QrySignatureBo2 qrySignatureBo2 = new QrySignatureBo2();
            qrySignatureBo2.setCityId(cityId);
            qrySignatureBo2.setCountyId(countyId);
            qrySignatureBo2.setSignName(signName);
            qrySignatureBo2.setEcName(ecName);
            qrySignatureBo2.setCheckState(checkState);
            qrySignatureBo2.setIndustryType(industryType);
            qrySignatureBo2.setCustomerType(customerType);
            signatureInfoList = sms5gSignatureMapper.qrySignatureInfo(qrySignatureBo2);
            if(!CollectionUtils.isEmpty(signatureInfoList)){
                for(SignatureInfo signatureInfo: signatureInfoList){
                    List<String> cityNameList = sms5gSignatureMapper.qryCityName(signatureInfo.getCityId());
                    cityName = cityNameList.get(0);

                    if(StringUtils.isNotBlank(signatureInfo.getCountyId())){
                        countyName = sms5gSignatureMapper.qryCountyName(signatureInfo.getCountyId());
                    }else{
                        countyName = "-";
                    }
                    signatureInfo.setCityName(cityName);
                    signatureInfo.setCountyName(countyName);
                }
            }
            pageInfo = new PageInfo<>(signatureInfoList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return pageInfo;
    }


    /**
     * @desc 查询5g签名通道号
     * @param cityId 
     * @return java.util.List<java.lang.String>
     * @author l
     * @date 2021/6/16 10:18
     */
    @Override
    @SystemServiceLog(description = "根据城市id查询扩展码信息", value = false)
    public List<String> find5gExtIdByCityId(String cityId) {
        List list = new ArrayList();
        try {
            list = sms5gSignatureMapper.find5gExtIdByCityId(cityId);
            log.error("根据城市id查询扩展码信息：" + cityId);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据城市id查询扩展码信息：" + cityId);
            throw new SmsException("根据城市id查询扩展码信息：" + cityId);
        }
        return list;
    }
    
    /**
     * @desc 查询签名信息列表
     * @param qrySignatureBo 
     * @return java.util.List<com.nl.controller.sms5g.bean.po.SignatureInfo>
     * @author l
     * @date 2021/6/16 10:19
     */
    @Override
    public BaseResult<List<SignatureInfo>> qrySignatureList2(QrySignatureBo2 qrySignatureBo) {
        BaseResult baseResult = BaseResult.newFailInstance();
        List<SignatureInfo> signatureInfoList = new ArrayList<>();
        String cityId = qrySignatureBo.getCityId();
        String cityName = "";
        String countyName = "";
        if("25".equals(cityId)){
            qrySignatureBo.setCityId("");
            qrySignatureBo.setCountyId("");
        }
        try {
            signatureInfoList = sms5gSignatureMapper.qrySignatureInfo(qrySignatureBo);
            if(!CollectionUtils.isEmpty(signatureInfoList)){
                for(SignatureInfo signatureInfo:signatureInfoList){
                    String checkState = signatureInfo.getCheckState();
                    String openAccount = signatureInfo.getOpenAccount();
                    String isArrearage = signatureInfo.getIsArrearage();
                    switch (checkState){
                        case "0":
                            checkState = "待审核";
                            break;
                        case "1":
                            checkState = "审核通过";
                            break;
                        case "-1":
                            checkState = "审核不通过";
                            break;
                    }

                    switch (openAccount){
                        case "0":
                            openAccount = "未开户";
                            break;
                        case "1":
                            openAccount = "已开户";
                            break;
                    }

                    switch (isArrearage){
                        case "0":
                            isArrearage = "未欠费";
                            break;
                        case "1":
                            isArrearage = "已欠费";
                            break;
                    }

                    List<String> cityNameList = sms5gSignatureMapper.qryCityName(signatureInfo.getCityId());
                    cityName = cityNameList.get(0);

                    if(StringUtils.isNotBlank(signatureInfo.getCountyId())){
                        countyName = sms5gSignatureMapper.qryCountyName(signatureInfo.getCountyId());
                    }else{
                        countyName = "-";
                    }
                    signatureInfo.setCityName(cityName);
                    signatureInfo.setCountyName(countyName);

                    signatureInfo.setCheckState(checkState);
                    signatureInfo.setOpenAccount(openAccount);
                    signatureInfo.setIsArrearage(isArrearage);

                }
            }

            baseResult.setRetCode(GlobalConstants.SUCCESS_STRING);
            baseResult.setRetMsg("查询签名信息列表成功");
            baseResult.setData(signatureInfoList);
        }catch (Exception e){
            e.printStackTrace();
            baseResult.setRetCode(GlobalConstants.EXCEPTION_STRING);
            baseResult.setRetMsg("查询签名信息列表出现异常："+e.getMessage());
        }
        return baseResult;
    }

    @Override
    public int removeSign(String signId) {
        return sms5gSignatureMapper.removeSign(signId);
    }

    @Override
    public BaseResult reApplySign(String signId) {
        BaseResult baseResult = BaseResult.newFailInstance();
        UpdateSignatureBo updateSignatureBo = new UpdateSignatureBo();
        updateSignatureBo.setSignId(signId);
        updateSignatureBo.setCheckState("0");
        try {
            sms5gSignatureMapper.updateSignature(updateSignatureBo);
            baseResult.setRetCode(GlobalConstants.SUCCESS_STRING);
            baseResult.setRetMsg("重新发起申请成功");

        }catch (Exception e){
            baseResult.setRetCode(GlobalConstants.EXCEPTION_STRING);
            baseResult.setRetMsg("重新发起申请发生异常："+e.getMessage());
        }
        return baseResult;
    }


}
